<?php
// app/controller/Crawler.php
namespace app\controller\wow;

use app\FrontendController;
use app\model\wow\WupinModel;
use think\facade\Request;
use think\facade\Db;


class Wupin extends FrontendController
{

    //访问链接：https://tp.x-mii.com/wow.Wupin/index
    public function index(){

        // return 1;
        $_GPC = Request::param(  );
        $filter = [
            'page'=>1,
            'name'=>''
        ];
        if( isset( $_GPC['keyword'] ) ){
            $filter['keyword'] = $_GPC['keyword'];
            
        }
        if( isset( $_GPC['page'] ) ){
            $filter['page'] = $_GPC['page'];
            
        }
        
        $listRows = 15; // 每页显示数量
        $currentPage = $filter['page']; // 获取当前页，默认为1
        $offset = ($currentPage - 1) * $listRows; // 计算偏移量

        $list = Db::table('wow_wupin')->where('name', 'like', '%' . $filter['keyword'] . '%')
                ->limit($offset, $listRows )
                ->select();
        return $list;

    }

    //访问链接：https://tp.x-mii.com/wow.Wupin/detail
    public function detail( ){

        $_GPC = Request::param(  );

        $id = intval( $_GPC['id'] );
        // return $id;
        
        $detail = Db::table('wow_wupin')->where('id', $id)->find();
        // return json_encode( $detail );

        // todo 如果detail字段 为空， 先抓取

        if( empty( $fetail->detail ) ){
            $html = WupinModel::zhuaquDetail( $detail['item_id'] );
            $detail['detail'] = $html;
            WupinModel::update(['detail' => $html, 'id' => $id ]);
        }


        return json_encode( $detail );
        // return $detail ;
    }

}